package com.example.mybiadushoujiweishi.activity.acivitys.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.BBX;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyItemTouchCallback;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.Myuitl;



import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/5/23 0023.
 */

public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyviewHolder> implements MyItemTouchCallback.ItemTouchAdapter{
    private Context context;
    List<BBX> list;


    public RecyclerviewAdapter(List<BBX> list) {
        this.list = list;
    }


    @Override
    public MyviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bbxitmel, parent, false);
        MyviewHolder myviewHolder = new MyviewHolder(view);
        return myviewHolder;
    }

    @Override
    public void onBindViewHolder(MyviewHolder holder, int position) {
        BBX bbx = list.get(position);

        /*holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyApplication.getContext(), FamilyguardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MyApplication.getContext().startActivity(intent);
            }
        });*/



        Drawable drawable = Myuitl.getBBXIcon(bbx.getIcon());
        holder.imgv.setImageDrawable(drawable);

        holder.textv.setText(bbx.getName());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onMove(int fromPosition, int toPosition) {
        if (fromPosition==list.size()-1 || toPosition==list.size()-1){
            return;
        }
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(list, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(list, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onSwiped(int position) {
        list.remove(position);
        notifyItemRemoved(position);

    }


    //先创建内部类

    public class MyviewHolder extends RecyclerView.ViewHolder {

        public ImageView imgv;
        public TextView textv;


        public MyviewHolder(View itemView) {
            super(itemView);
            WindowManager wm = (WindowManager) MyApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
            layoutParams.height = width/4;
            itemView.setLayoutParams(layoutParams);
            imgv = (ImageView) itemView.findViewById(R.id.img);
            textv = (TextView) itemView.findViewById(R.id.textfont);

        }
    }


}
